package com.lagou.demo.controller;

import com.lagou.demo.service.IDemoService;
import com.lagou.edu.mvcframework.annotations.LagouAutowired;
import com.lagou.edu.mvcframework.annotations.LagouController;
import com.lagou.edu.mvcframework.annotations.LagouRequestMapping;
import com.lagou.edu.mvcframework.annotations.LagouSecurity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@LagouController
@LagouRequestMapping("/demo")
@LagouSecurity({"zhang"})
public class DemoController {


    @LagouAutowired
    private IDemoService demoService;


    /**
     * 测试： http://localhost:8080/demo/query?name=zhangsan&username=zhang
     *       http://localhost:8080/demo/query?name=zhangsan&username=tang
     *       http://localhost:8080/demo/query?name=zhangsan&username=lin
     * URL: /demo/query?name=lisi
     * @param request
     * @param response
     * @param name
     * @return
     */
    @LagouRequestMapping("/query")
    @LagouSecurity({"tang","lin"})
    public String query(HttpServletRequest request, HttpServletResponse response,String name) throws IOException {
        String s = demoService.get(name);
        response.getWriter().write(request.getParameter("username") + " have permission");
        return s;
    }

    /**
     * 测试： http://localhost:8080/demo/query2?name=zhangsan&username=zhang
     *       http://localhost:8080/demo/query2?name=zhangsan&username=tang
     *
     * 此方法 只有 zhang 用户有权限访问
     * URL: /demo/query2?name=lisi
     * @param request
     * @param response
     * @param name
     * @return
     */
    @LagouRequestMapping("/query2")
    public String query2(HttpServletRequest request, HttpServletResponse response,String name) throws IOException {
        String s = demoService.get(name);
        response.getWriter().write(request.getParameter("username") + " have permission");
        return s;
    }
}
